<?php
/**
 * Trainingofferadmin_Data_Trainingofferslist
 *
 * @author adrian
 */
class Trainingofferadmin_Data_Trainingofferslist implements Trainingofferadmin_Data_Mainable, 
        Trainingofferadmin_Data_Preparable, Trainingofferadmin_Data_Templatable, 
        Trainingofferadmin_Data_Tokenable{
    
    public $database;
    public $random_token;
    public $database_query;
    
    public function create_token() {
        $this->random_token = new Core_Data_Tokenizer;
        return $this->random_token->create_token();
    }

    public function get_template_name() {
        $template = SERVER_ROOT."trainingofferadmin/views/trainingofferslist.php";
        return $template;
    }

    public function main(array $variables = null) {
        $this->database = new Core_Driver_MysqlImproved;
        $page['template'] = $this->get_template_name();
        $page['csrf_token'] = $this->create_token();
        $this->prepare_query($variables);
        //echo $this->database_query;
        //die();
        $this->database->connect();
        $this->database->prepare($this->database_query);
        $this->database->query();
        $page['offer_data'] = array();
        while($training_offer_data = $this->database->fetch('array')){
            $page['offer_data_key'][$training_offer_data['training_title']] = $training_offer_data; 
        }
        //var_dump($page);
        return $page;
    }

    public function prepare_query(array $variables = null) {
        $this->database_query = "SELECT "
                . "training_offer.id"
                . ", "
                . "training_offer.training_title"
                . ", "
                . "page_main_category.main_category_name"
                . ", "
                . "page_sub_category.sub_category_name"
                . " "
                . "FROM"
                . " "
                . "training_offer"
                . ", "
                . "page_main_category"
                . ", "
                . "page_sub_category"
                . " "
                . "WHERE"
                . " "
                . "training_offer.page_main_category_id"
                . " = "
                . "page_main_category.id"
                . " AND "
                . "training_offer.page_sub_category_id"
                . " = "
                . "page_sub_category.id"
                . " AND "
                . "training_offer.user_id"
                . " = "
                .$_SESSION['user_id']
                . " ORDER BY "
                . "training_title"
                . " ASC"
                . ";";
    }

}
